package crond

import (
	"context"
	"sale-admin/config/mysql"
	"sale-admin/internal/app/web/service"
	"sale-admin/internal/dao"

	"github.com/golang-module/carbon/v2"
)

func GoodsClickSummary() error {
	ctx := context.Background()
	db := mysql.NewDB()
	// 昨日
	yesterday := carbon.Yesterday().ToDateString()
	YesterRobotList, robotCount, _ := dao.Robot{}.RobotList(ctx, db, "", yesterday, "", "", "", "", "", "")
	if robotCount == 0 {
		return nil
	}
	for _, v := range YesterRobotList {
		service.RobotDailyReport{}.UpdateGoodsClick(ctx, db, v.RobotName, yesterday)
	}
	// 前天
	dayBeforeYesterday := carbon.Now().SubDays(2).ToDateString()
	RobotList, robotCount, _ := dao.Robot{}.RobotList(ctx, db, "", dayBeforeYesterday, "", "", "", "", "", "")
	if robotCount == 0 {
		return nil
	}
	for _, v := range RobotList {
		service.RobotDailyReport{}.UpdateGoodsClick(ctx, db, v.RobotName, dayBeforeYesterday)
	}
	return nil
}

// 按商品分组的商品点击汇总
func GoodsClickSummaryByGoods() error {
	ctx := context.Background()
	db := mysql.NewDB()
	// 昨日
	yesterday := carbon.Yesterday().ToDateString()
	YesterRobotList, robotCount, _ := dao.Robot{}.RobotList(ctx, db, "", yesterday, "", "", "", "", "", "")
	if robotCount == 0 {
		return nil
	}

	for _, v := range YesterRobotList {
		service.GoodsDailyReport{}.UpdateGoodsClick(ctx, db, v.RobotName, "", yesterday)
	}

	// 前天
	dayBeforeYesterday := carbon.Now().SubDays(2).ToDateString()
	RobotList, robotCount, _ := dao.Robot{}.RobotList(ctx, db, "", dayBeforeYesterday, "", "", "", "", "", "")
	if robotCount == 0 {
		return nil
	}
	for _, v := range RobotList {
		service.GoodsDailyReport{}.UpdateGoodsClick(ctx, db, v.RobotName, "", dayBeforeYesterday)
	}
	return nil
}
